struct VertexOutput {
    @builtin(position) clip_pos: vec4f,
    @location(0) color: vec4f,
    @location(1) pos0: vec2f,
}

struct Camera {
    pos: vec2f,
    scale: vec2f
}
@group(0) @binding(0)
var<uniform> camera: Camera;

var<private> pos0_list: array<vec2f, 4> = array(
    vec2f(-1.0, -1.0),
    vec2f(1.0, -1.0),
    vec2f(-1.0, 1.0),
    vec2f(1.0, 1.0),
);

@vertex
fn vs_main(
    @builtin(vertex_index) index: u32,
    @location(0) min: vec2f,
    @location(1) max: vec2f,
    @location(2) color: u32,
) -> VertexOutput {
    var out: VertexOutput;
    let i = index % 2;
    let j = index / 2;
    var pos: vec2f;
    if i == 0 {
        pos.x = min.x;
    } else {
        pos.x = max.x;
    }
    if j == 0 {
        pos.y = min.y;
    } else {
        pos.y = max.y;
    }
    let clip_pos = (pos - camera.pos) * camera.scale;
    out.clip_pos = vec4f(clip_pos, 0.0, 1.0);
    out.pos0 = pos0_list[index];
    out.color = unpack4x8unorm(color);
    return out;
}

@fragment
fn fs_main(
    in: VertexOutput
) -> @location(0) vec4f {
    if length(in.pos0) < 1.0 {
        return in.color;
    } else {
        discard;
    }
}